package com.tuyan.aixiang.component;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.tuyan.aixiang.R;

import androidx.annotation.Nullable;
import butterknife.BindView;
import butterknife.ButterKnife;

public class BYTabItem extends LinearLayout {



    @BindView(R.id.iv_tab_item_icon)
    ImageView ivIcon;

    @BindView(R.id.tv_tab_item_text)
    TextView ivText;

    private OnTabClickListener listener;

    private int index;

    private int text;

    private int normalIcon;

    private int enableIcon;

    private Context context;

    public BYTabItem(Context context) {
        this(context, null);
    }

    public BYTabItem(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BYTabItem(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        View contentView  = LayoutInflater.from(context).inflate(R.layout.by_tab_item, this);
        ButterKnife.bind(this, contentView);
        setOnClickListener(v -> {
            if(null != this.listener) {
                Log.d("TabItem click ", "" + this.index);
                listener.apply(this.index);
            }

        });

        LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 1.0f); // layout weight 1.0f
        setLayoutParams(lp);
    }

    public void setup(int text, int normalIcon, int enableIcon) {
        this.text = text;
        this.normalIcon = normalIcon;
        this.enableIcon = enableIcon;
        ivText.setText(text);
        this.disable();
    }

    public void enable() {
        ivIcon.setImageDrawable(context.getDrawable(this.enableIcon));
        ivText.setTextColor(context.getResources().getColor(R.color.colorPrimary));
    }

    public void disable() {
        ivIcon.setImageDrawable(context.getDrawable(this.normalIcon));
        ivText.setTextColor(context.getResources().getColor(R.color.textLight));
    }

    public void setIndex(int idx) {
        this.index = idx;
    }

    public void setOnTabClickListener(OnTabClickListener listener) {
        this.listener = listener;
    }

    public interface OnTabClickListener{
        void apply(int idx);
    }

}
